#pragma once

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <atomic>
#include <fstream>
#include <vector>
#include <boost/algorithm/string.hpp>

namespace ns_util
{
    const std::string temp_path = "./temp/";
    class PathUtil
    {
    public:
        static std::string AddSuffix(const std::string& filename,const std::string& suffix)
        {
            std::string path_name = temp_path;
            path_name += filename;
            path_name += suffix;
            return path_name;
        }
        static std::string Src(const std::string& filename)
        {
            return AddSuffix(filename,".cpp");
        }
        static std::string Exe(const std::string& filename)
        {
            return AddSuffix(filename,".exe");
        }
        static std::string CompilerError(const std::string& filename)
        {
            return AddSuffix(filename,".compiler_error");
        }
        static std::string Stdin(const std::string& filename)
        {
            return AddSuffix(filename,".stdin");
        }
        static std::string Stdout(const std::string& filename)
        {
            return AddSuffix(filename,".stdout");
        }
        static std::string Stderr(const std::string& filename)
        {
            return AddSuffix(filename,".stderr");
        }
    };

    class TimeUtil
    {
    public:
        static std::string GetTimeStamp()
        {
            struct timeval _time;
            gettimeofday(&_time,nullptr);
            return std::to_string(_time.tv_sec);
        }
        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time,nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_sec / 1000);
        }
    };


    class FileUtil
    {
    public:
        static bool IsFileExists(const std::string path_name)
        {
            struct stat st;
            if(stat(path_name.c_str(),&st) == 0)
            {
                return true;
            }
            return false;
        }
        static std::string UniqFileName()
        {
            static std::atomic_uint id(0);
            id++;
            std::string ms = TimeUtil::GetTimeMs();
            std::string uniq_id = std::to_string(id);
            return ms + "_" + uniq_id;
        }
        static bool WriteFile(const std::string& target,const std::string& content)
        {
            std::ofstream out(target);
            if(!out.is_open())
            {
                return false;
            }
            out.write(content.c_str(),content.size());
            out.close();
            return true;
        }
        static bool ReadFile(const std::string& target,\
            std::string* content,bool keep = false)
        {
            (*content).clear();
            std::ifstream in(target);
            if(!in.is_open())
            {
                return false;
            }
            std::string line;
            while(std::getline(in,line))
            {
                (*content) += line;
                (*content) += (keep ? "\n" : "");
            }
            in.close();
            return true;
        }
    };

    class StringUtil
    {
    public:
        static void SplitString(const std::string& line,std::vector<std::string>* tokens,const std::string& sep)
        {
            boost::split((*tokens),line,boost::is_any_of(sep),boost::algorithm::token_compress_on);
        }
        static bool ComString(const std::string& str1,const std::string& str2)
        {
            std::vector<std::string> tokens1;
            std::vector<std::string> tokens2;
            SplitString(str1,&tokens1," \n\r");
            SplitString(str2,&tokens2," \n\r");
            int n = tokens1.size();
            for(int i = 0;i < n;i++)
            {
                std::cout << "tokens1[" << i << "]" << tokens1[i] << " 1"<< std::endl;
            }
            for(int i = 0;i < n;i++)
            {
                std::cout << "tokens2[" << i << "]" << tokens2[i] << " 1"<< std::endl;
            }
            for(int i = 0;i < n;i++)
            {
                if(tokens1[i] == tokens2[i])
                {
                    std::cout << "Yes" << std::endl;
                }
                else
                {
                    std::cout << "No" << std::endl;
                }
            }
            for(int i = 0;i < n;i++)
            {
                if(tokens1[i] != tokens2[i])
                {
                    return false;
                }
            }
            return true;
        }
    };
}


